package org.gnuhpc.task;

import java.util.List;

import org.gnuhpc.R;
import org.gnuhpc.activity.Search;
import org.gnuhpc.location.MyMapView;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class SearchTask extends AsyncTask<String, Void, Void> {
	private MyMapView mMapView;
	private Context mContext;
	private static final String LOG_TAG = "ShowLocation";
	private List<Address> resultAddresses;
	private String[] address = {};
	private String[] location = {};
	private CharSequence hint = null;
	
	public SearchTask(MyMapView mMapView, Context mContext) {
		super();
		this.mMapView = mMapView;
		this.mContext = mContext;
	}

	@Override
	protected void onPostExecute(Void result) {
		
		if (hint!=null) {
			Toast.makeText(mContext,hint, Toast.LENGTH_SHORT).show();
		}
		else {
			resultHandler(resultAddresses);	
		}
		super.onPostExecute(result);
	}

	@Override
	protected Void doInBackground(String... params) {
		getLocaton(params[0]);
		return null;
	}
	
	private void getLocaton(String placeName)
	{
		int numberOptions = 8;
		Geocoder gcoder = new Geocoder(mContext);
		try {
			resultAddresses =  gcoder.getFromLocationName(placeName, numberOptions);
			Log.d(LOG_TAG, "Address: " + resultAddresses +" Place:" + placeName);

		} catch (Exception e) {
			hint = new String(mContext.getString(R.string.map_service_unavailable_now));
			Log.e("Geocoder", "I/O Failure; is network available?",e);
		}		
	}
	
	private void resultHandler(List<Address> resultAddresses) {
		
		if (resultAddresses.isEmpty()) {
			Toast.makeText(mContext,R.string.Search_NoInfo , Toast.LENGTH_SHORT);				
		}
		else if(resultAddresses.size()>1){
			address = new String[resultAddresses.size()]; 
			location = new String[resultAddresses.size()];
			Intent it = new Intent(mContext,Search.class);

			for (int i = 0; i < resultAddresses.size(); i++) {
				StringBuilder sb = new StringBuilder();
				Address result = resultAddresses.get(i);
				for (int j = 0; j < result.getMaxAddressLineIndex(); j++) {			
					Log.d(LOG_TAG, "j:"+j +" address:" + result.getAddressLine(j));
					sb.append(result.getAddressLine(j));
				}			
				sb.append("\n").append(result.getLocality()).append("\n");
				if(result.getFeatureName()!=null){
					sb.append(result.getFeatureName()).append("\n");
				}
				sb.append(result.getCountryName());
				address[i] = sb.toString();
				location[i] = new String(result.getLatitude()+"\n"+result.getLongitude());
			}
			it.putExtra("ADDRESS", address);
			it.putExtra("LOCATION", location);
			mContext.startActivity(it);
		}
		else {
			double lat = resultAddresses.get(0).getLatitude();
			double lon = resultAddresses.get(0).getLongitude();
			GeoPoint geoPoint = new GeoPoint((int)(lat*1e6), (int)(lon*1e6));
			mMapView.animateTo(geoPoint);
		}
	}

	


}
